package entity
{	
	import Common.BitmapSprite;
	
	import Sync.*;
	
	import flash.geom.Point;
	import flash.text.*;
	import flash.ui.Keyboard;
	import flash.utils.*;
	
	import logic.*;
	
	public class EntityPlayer extends Entity
	{
		private var netID:int = -1;
		private var moveDir:int = -1;
		private var sprite:BitmapSprite = null;
		private var bullets:Array = new Array();
		private var life:int = 100;
		private var lifetxt:TextField;
		
		public function EntityPlayer(handle:int)
		{
			super(handle);
			
			sprite = new BitmapSprite();
			sprite.x = -16;
			sprite.y = -16;
			addChild(sprite);
			
			
			lifetxt = new TextField();

			lifetxt.width = 50;
			lifetxt.text = "" + life;
			
			lifetxt.x = sprite.x;
			lifetxt.y = sprite.y - 15;		
			
			addChild(lifetxt);
		}
		
		public function setNetID(id:int):void
		{
			netID = id;
		}
		
		public function getNetID():int
		{
			return netID;	
		}
		
		public function getLifeNum():int
		{
			return life;
		}
		
		public function setLifeNum(num:int):void
		{
			life = num;
		
			if (life < 0)
				life = 0;
			else if (life > 100)
				life = 100;
			
			lifetxt.text = "" + life;
		}
		
		override public function onMessage(sender:int, instruct:int, stream:Stream):void
		{
			if (instruct == ConstValue.INST_KEY_DOWN)
			{
				if (getLifeNum() == 0)
					return;		
				
				var keyCode:int = stream.readInt();
				
				var selfID:int = GameLogic.getInstance().getSelfPlayerHandleID();
				
				trace("key down receive" + getTimer());
				
				if (keyCode == Keyboard.W || keyCode == Keyboard.S || keyCode == Keyboard.A || keyCode == Keyboard.D)
				{
					moveDir = keyCode;
				}
			}
			else if (instruct == ConstValue.INST_KEY_UP)
			{
				if (getLifeNum() == 0)
					return;		
				
				var keyCode:int = stream.readInt();
				
				if (keyCode == Keyboard.W || keyCode == Keyboard.S || keyCode == Keyboard.A || keyCode == Keyboard.D)
				{
					if (keyCode == moveDir)
					{
						moveDir = -1;
					}
				}
			}
			else if (instruct == ConstValue.INST_FIRE)
			{
				if (getLifeNum() == 0)
					return;		
				
				var aimX:int = stream.readInt();
				var aimY:int = stream.readInt();
				
				var dir:Point = new Point(aimX - this.x, aimY - this.y);
				
				if (dir.length > 0)
				{
					dir.normalize(1);
					
					var bullet:Object = new Object();
					var bulletSpr:BitmapSprite = new BitmapSprite();
					bulletSpr.scaleX = 0.5;
					bulletSpr.scaleY = 0.5;
					bulletSpr.x = this.x - 8;
					bulletSpr.y = this.y - 8;
					
					stage.addChild(bulletSpr);
					
					bullet.spr = bulletSpr;
					bullet.dir = dir;
					bullet._parentHandle = m_handle;
					
					bullets.push(bullet);
				}
			}
		}
		
		public function collTest(posX:Number, posY:Number):Boolean
		{
			var dis:Point = new Point(posX - sprite.x, posY - sprite.y);
			if (dis.length < 5)
			{
				return true;
			}
			
			return false;
		}
		
		override public function logicStep(frameTime:int):void
		{
			if (moveDir != -1)
			{
				var Speed:Number = 0.8;
				
				if (moveDir == Keyboard.W)
				{
					this.y = this.y - Speed;
				}
				else if (moveDir == Keyboard.S)
				{
					this.y = this.y + Speed;
				}
				else if (moveDir == Keyboard.A)
				{
					this.x = this.x - Speed;
				}
				else if (moveDir == Keyboard.D)
				{
					this.x = this.x + Speed;
				}
				
				if (this.x > 800)
					this.x = 800;
				if (this.x < 0)
					this.x = 0;
				if (this.y > 600)
					this.y = 0;
				if (this.y < 0)
					this.y = 0;
				
				
				
			}
			
			for (var i:int = 0; i < bullets.length; i++)
			{
				var bullet:Object = bullets[i];
				bullet.spr.x = bullet.spr.x + bullet.dir.x * 3;
				bullet.spr.y = bullet.spr.y + bullet.dir.y * 3;
				
				if (bullet.spr.x > 810 ||
					bullet.spr.y > 610 ||
					bullet.spr.x < -10 ||
					bullet.spr.y < -10)
				{
					stage.removeChild(bullet.spr);
					bullets.splice(i, 1);
					i--;
				}
				else
				{
					var tmpHandles:Array = EntityManager.getInstance().getPlayerHandles();
					
					for (var count = 0; count < tmpHandles.length; ++count)
					{
						var handle:int = tmpHandles[count];
						
						var entityPlayer:EntityPlayer = EntityManager.getInstance().getEntity(handle) as EntityPlayer;
						
						if (bullet.spr.x >= entityPlayer.x - 20 && bullet.spr.y >= entityPlayer.y - 20
						 && bullet.spr.x <= entityPlayer.x + 20 && bullet.spr.y <= entityPlayer.y + 20
						 && bullet._parentHandle != handle && entityPlayer.getLifeNum() > 0)
						{
							stage.removeChild(bullet.spr);
							bullets.splice(i, 1);
							i--;
							
							entityPlayer.setLifeNum(entityPlayer.getLifeNum() - 10);
							
							if (entityPlayer.getLifeNum() == 0)
								entityPlayer.visible = false;
							
							break;
						}
						
					}
				
				}
			}
		}
	}
}